package com.vast.ui.component.socketinterface.launchar;

import java.util.concurrent.Executor;

import com.vast.ui.component.socketinterface.listner.IReceiveListener;
import com.vast.ui.component.socketinterface.request.RequestQueue;
import com.vast.ui.component.socketinterface.serverblocking.BlockingServer;
import com.vast.ui.component.socketinterface.servercommon.IServer;
import com.vast.ui.component.util.ConsoleLogger;
import com.vast.ui.component.util.ConsoleLoggerSingleton;
import com.vast.ui.component.util.Logger;

public class InterfaceLaunchar {
	public  int          serverPort = 30000;
	public  IServer      server;
	public  RequestQueue sendRequestQueue;
	private boolean      isServerActive = false;
	private final Executor executor;
	public  final Logger   logger;
	
	public InterfaceLaunchar(int serverPort) {
		this.serverPort = serverPort;
		this.logger     = ConsoleLoggerSingleton.getInstance();
		this.executor   = new Executor() {
			public void execute(Runnable r) {
				new Thread(r).start();
			}
		};
		this.server = new BlockingServer (serverPort, sendRequestQueue, executor);
	}
	
	public void openConnection() {
		if(!isServerActive()) {
			setServerActive(true);
			server.openConnection();
		}
	}
	
	public void closeConnection() {
		server.closeConnection();
	}
	
	public void addListener(IReceiveListener listener){
		server.addListener(listener);
	}
	
	 public void removeListener(IReceiveListener listener) {
		 server.removeListener(listener);
	 }

	public synchronized void setServerActive(boolean isServerActive) {
		this.isServerActive = isServerActive;
	}

	public boolean isServerActive() {
		return isServerActive;
	}
}
